package com.qianxun.module.datasource1.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianxun.module.datasource1.admin.entity.SysUserMenuRoleRelation;
import com.qianxun.module.datasource1.admin.mapper.SysUserMenuRoleRelationMapper;
import com.qianxun.module.datasource1.admin.service.SysUserMenuRoleRelationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户角色mapping 服务实现类
 * </p>
 *
 * @author admin
 * @since 2019-11-13
 */
@Service
public class SysUserMenuRoleRelationServiceImpl extends ServiceImpl<SysUserMenuRoleRelationMapper, SysUserMenuRoleRelation> implements SysUserMenuRoleRelationService {

    @Override
    public List<Long> listRoleIdByUserId(Long userId) {
        return this.baseMapper.listRoleIdByUserId(userId);
    }

    @Override
    public void removeByUserId(Long userId) {
        Wrapper wrapper = new QueryWrapper<SysUserMenuRoleRelation>().lambda().eq(SysUserMenuRoleRelation::getUserId ,userId);
        remove(wrapper);
    }

    @Override
    public void setAuthority(Long userId, Long[] roleIds) {
        //删除角色
        removeByUserId(userId);

        List<SysUserMenuRoleRelation> list = new ArrayList<>(roleIds.length);
        for(int i = 0 ;i < roleIds.length ;++i){
            if(roleIds[i] == 0){
                continue;
            }
            SysUserMenuRoleRelation sysUserRoleRelation = new SysUserMenuRoleRelation();
            sysUserRoleRelation.setUserId(userId).setRoleId(roleIds[i]);
            list.add(sysUserRoleRelation);
        }
        saveBatch(list);
    }

    @Override
    public Set<String> getPermissionUrl(long userId) {
        return baseMapper.selectMenuUrlListByUserId(userId);
    }
}
